package com.hmall.es.listener;

import com.alibaba.fastjson.JSON;
import com.hmall.es.service.ISearchService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 商品上下架同步ES的监听类
 */
@Component
public class DownOrUpListener {

    @Autowired
    private ISearchService searchService;

    @RabbitListener(queues = "down_or_up_queue")
    public void downOrUpItemToEs(String message){
        //1.解析消息
        Map map = JSON.parseObject(message, Map.class);
        Long id = (Long) map.get("id");
        Integer status = (Integer) map.get("status");


        //2.执行同步索引库
        if(status==1){
            //新增数据到ES中
            searchService.saveItemToES(id);
        }else if(status==2){
            //删除ES索引库中的数据
            searchService.delItemFromES(id);
        }
    }
}
